package com.xyulu.exception;

import com.xyulu.entity.common.MessageConstant;
import com.xyulu.entity.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author Xyulu
 * @Date 2021/1/30 21:24
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 全局处理异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handler(Exception e){
        logger.error("exception is {}",e);
        return new Result(400, MessageConstant.EXCEPTION);
    }

    @ExceptionHandler(value = AuthorizationException.class)
    public String defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) throws Exception{
        return "error/504";
    }


}
